package com.sunny.ynhz.ui.lake;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sunny.ynhz.PatrolMapActivity;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.PatrolRouteAdapter;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.PartrolRouteBean;
import com.sunny.ynhz.ui.base.CustomLayFragment;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by huhuan123 on 2018/5/21.
 */

public class PatrolBasinRiverRouteFragment extends CustomLayFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    private List<PartrolRouteBean.DataBean.ChildrenBean> content = new ArrayList<>();
    private RecyclerView rv_patrol_river;
    private SwipeRefreshLayout swipeRefreshLayout_ly;
    private PatrolRouteAdapter patrolProblemAdapter;

    public void initData() {
        hasLoaded = true;
        String type = getArguments().getString("type");
        HttpManager.getInstance().getBasinDetailSeasurgeMsg(type).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(getContext(), new ApiSubscriber.HttpOnResultListener<PartrolRouteBean>() {
            @Override
            public void onNext(PartrolRouteBean partrolRouteBean) {
                List<String> list = new ArrayList<>();
                list.clear();
                content.clear();
                patrolProblemAdapter.notifyDataSetChanged();
                swipeRefreshLayout_ly.setRefreshing(false);
                patrolProblemAdapter.setEnableLoadMore(false);
                List<PartrolRouteBean.DataBean> data = partrolRouteBean.getData();
                for (int i = 0; i < data.size(); i++) {
                    List<PartrolRouteBean.DataBean.ChildrenBean> children = data.get(i).getChildren();
                    for (int j = 0; j < children.size(); j++) {
                        PartrolRouteBean.DataBean.ChildrenBean childrenBean = children.get(j);
                        String time = childrenBean.getTime();
                        if (!list.contains(time)) {
                            list.add(time);
                            childrenBean.setShow(true);
                        } else childrenBean.setShow(false);

                    }
                    content.addAll(children);
                }
                patrolProblemAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                swipeRefreshLayout_ly.setRefreshing(false);
                patrolProblemAdapter.loadMoreComplete();
            }

            @Override
            public void onCompleted() {
                swipeRefreshLayout_ly.setRefreshing(false);
                patrolProblemAdapter.loadMoreComplete();
            }

            @Override
            public void onstart() {

            }
        }));

    }

    @Override
    public void setUpView() {
        rv_patrol_river = mContentView.findViewById(R.id.rv_patrol_river);
        swipeRefreshLayout_ly = mContentView.findViewById(R.id.id_swipe_ly);
        swipeRefreshLayout_ly.setOnRefreshListener(this);
        rv_patrol_river.setLayoutManager(new LinearLayoutManager(getContext()));
        patrolProblemAdapter = new PatrolRouteAdapter(content);
        rv_patrol_river.setAdapter(patrolProblemAdapter);
        patrolProblemAdapter.setOnLoadMoreListener(this);
        patrolProblemAdapter.setOnItemClickListener((adapter, view, position) -> {
            PartrolRouteBean.DataBean.ChildrenBean childrenBean = content.get(position);
            Intent intent = new Intent(getContext(), PatrolMapActivity.class);
            intent.putExtra("id", childrenBean.getPatrolId());
            startActivity(intent);
        });
    }

    @Override
    public int setLayoutId() {
        return R.layout.patrol_lake_fragment;
    }

    @Override
    public void setupData() {
        if (content.size() == 0) {
            swipeRefreshLayout_ly.setRefreshing(true);
            initData();
        }
    }

    @Override
    public void onRefresh() {
        initData();
    }

    @Override
    public void onLoadMoreRequested() {

    }
}


